package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Gdbg;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GdbgForm extends BasePage implements Serializable {
    private GenericManager<Gdbg, Long> gdbgManager;
    private Gdbg gdbg = new Gdbg();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setGdbgManager(GenericManager<Gdbg, Long> gdbgManager) {
        this.gdbgManager = gdbgManager;
    }

    public Gdbg getGdbg() {
        return gdbg;
    }

    public void setGdbg(Gdbg gdbg) {
        this.gdbg = gdbg;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        gdbgManager.remove(gdbg.getId());
        addMessage("gdbg.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            gdbg = gdbgManager.get(id);
        } else {
            gdbg = new Gdbg();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (gdbg.getId() == null || gdbg.getId() == 0);
        gdbg.setJbqk( jbqk );
        // set username who act this operation, get the user name from httprequest
        gdbg.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        gdbg.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        gdbg.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        gdbgManager.save(gdbg);

        String key = (isNew) ? "gdbg.added" : "gdbg.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 // set the id to the next page
            this.id = gdbg.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 